/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package petstore.component.mail.impl;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;
import petstore.component.mail.IMailComponent;

/**
 *
 * @author Tom Hayden
 */
public class CommonsMailComponent implements IMailComponent {

    private static final Logger logger = Logger.getLogger(CommonsMailComponent.class);
    private String smtpServer;
    
    public void setSmtpServer(String smtpServer) {
        this.smtpServer = smtpServer;
    }
    
    public void sendMail(String from, String to, String subject, String body) {
        HtmlEmail email = new HtmlEmail();
        try {
            email.setHostName(this.smtpServer);
            email.setFrom(from);
            email.addTo(to);
            email.setSubject(subject);
            email.setHtmlMsg(body);
            email.send();
        } catch (EmailException ex) {
            logger.error("erro no envio de email\n" +
                         "smtpServer: "+smtpServer+"\n"+
                         "from: "+from+"\n"+
                         "to: "+to+"\n" +
                         "subject: "+subject+"\n" +
                         "body: "+body, ex);
        }
    }

}
